package ksv.cron;

import api.common.Cst;
import ksv.cron.bean.SysCron;
import ksv.cron.v1.CronHandler;
import lombok.Getter;
import lombok.Setter;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @创建人 xiaojiancheng
 * @时间 2020/9/30
 * @描述 $
 */
public class CronModel {
    @Setter
    @Getter
    public static class DelayCron implements Delayed,Cloneable {
        private SysCron cron;
        private CronHandler cronHandler;
        private LocalDateTime executeTime;

        private boolean timeInvalid;

        @Override
        public long getDelay(TimeUnit unit) {
            //  距离当前时间还有多少时间才会执行
            LocalDateTime now = LocalDateTime.now();
            long second = Duration.between(now, executeTime).getSeconds();
            long convert = unit.convert(second, TimeUnit.SECONDS);
            return convert;
        }

        @Override
        public int compareTo(Delayed o) {
            DelayCron o1 = (DelayCron) o;
            long seconds = Duration.between(o1.getExecuteTime(), executeTime).getSeconds();
            return (int) seconds;
        }

        public String getExistsKey(){
            return getClass().getName()+cron.getId()+"::"+ Cst.L_D_T_FORMAT.format(executeTime);
        }

        @Override
        public Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }

    @Setter
    @Getter
    public static class R<E> {
        private int code;
        private String msg;
        private E data;
    }
}
